package com.haiyou.dc.thread;


import net.sf.jsqlparser.statement.select.Wait;

import java.util.Date;
import java.util.concurrent.CountDownLatch;

/**
 * @author pc
 */
public class TestWait {

    private static TestWait testWait = new TestWait();

    public static void main(String[] args) throws InterruptedException {

//        CountDownLatch countDownLatch = new CountDownLatch(1);
//        Thread thread1 = new Thread(() -> {
//            synchronized (testWait) {
//                try {
//                    System.out.println(new Date());
//                    System.out.println("--------------------->等待3秒");
//                    testWait.wait();
////                    countDownLatch.wait();
//                    System.out.println("=============================>");
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//        thread1.start();
//
//
////        thread1.join(3000);
//
//
//
//        Thread.sleep(3000);
//        synchronized (testWait) {
//            testWait.notify();
////            countDownLatch.countDown();
//        }


        Thread thread1 = new Thread(() -> {
            synchronized (Wait.class) {
                try {
                    System.out.println(new Date() + " Thread1 is running");
                    Wait.class.wait();
                    System.out.println(new Date() + " Thread1 ended");
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        thread1.start();

        Thread thread2 = new Thread(() -> {
            synchronized (Wait.class) {
                try {
                    System.out.println(new Date() + " Thread2 is running");
                    Wait.class.notify();
                    // Don't use sleep method to avoid confusing
                    for (long i = 0; i < 200000; i++) {
                        for (long j = 0; j < 100000; j++) {
                        }
                    }
                    System.out.println(new Date() + " Thread2 release lock");
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

            for (long i = 0; i < 200000; i++) {
                for (long j = 0; j < 100000; j++) {
                }
            }
            System.out.println(new Date() + " Thread2 ended");
        });

        // Don't use sleep method to avoid confusing
        for (long i = 0; i < 200000; i++) {
            for (long j = 0; j < 100000; j++) {
            }
        }
        thread2.start();


    }
}
